package com.servlets.task;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.GlobalConstants;
import com.core.protocols.IServiceLayerDelegate;
import com.managers.session.SessionManager;
import com.servicelayer.project.ProjectTasksListLayer;
import com.servlets.TaskTrackerServlet;

/**
 * Servlet implementation class ProjectDelete
 */
public class ProjectTaskDelete
        extends TaskTrackerServlet
        implements IServiceLayerDelegate {

    private String              redirection  = null;
    private HttpServletResponse httpResponse = null;



    @Override
    public <T> void responseResponseItemsList ( final List <T> items,
                                                final Object request,
                                                final Exception exception ) {

        if ( httpResponse == null ) {
            return;
        }

        if ( exception != null ) {

        }

        if ( redirection == null ) {
            return;
        }

        try {
            httpResponse.sendRedirect(redirection);
        } catch ( IOException e ) {
            e.printStackTrace();
        }

    }

    private static final long serialVersionUID = 1L;



    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProjectTaskDelete() {

        super();

    }



    @Override
    protected void doPost ( HttpServletRequest request,
                            HttpServletResponse response )
            throws ServletException, IOException {

        this.httpResponse = response;

        String taskId = request.getParameter(GlobalConstants.SERVLET_PARAM_KEY);

        ProjectTasksListLayer projectTasksListLayer = new ProjectTasksListLayer();
        projectTasksListLayer.setLayerDelegate(this);
        projectTasksListLayer.deleteObject(taskId);

    }



    @Override
    protected void doGet ( HttpServletRequest request,
                           HttpServletResponse response )
            throws ServletException, IOException {


        redirection = SessionManager.sharedInstance().lastExecutedURL();
        super.doGet(request, response);
    }

}
